package org.biodas.jdas.client.threads;

import org.apache.log4j.Logger;
import org.biodas.jdas.client.TypesClient;
import org.biodas.jdas.exceptions.DASClientException;
import org.biodas.jdas.schema.types.DASTYPES;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 02/12/11
 * Time: 10:49
 * To change this template use File | Settings | File Templates.
 */
public class TypesClientMulptipleSources extends ClientMultipleSources  {
    private static Logger logger = Logger.getLogger(TypesClientMulptipleSources.class);

    /**
     * Fetches data from multiple sources for a list of segments; segments can include ranges.
     * The invoking method is responsible for processing the results.
     * @param sources List of sources (url)
     * @param segments List of segments
     * @param results Map of (source name, (list of segments, DASTYPES responses)) one for each source successfully queried
     * @param errors  Map of (source name, (list of segments, exceptions)) one for each source raising an exception
     */
    public void fetchData(List<String> sources, final List<String> segments, final List<String> types, final Map<String, Map<List<String>, DASTYPES>> results, final Map<String, Map<List<String>, Exception>> errors) {
        for (final String source: sources) {
            this.runTask(new Runnable() {
                @Override
                public void run() {
                    try {
                        TypesClient typesClient = new TypesClient();
                        DASTYPES adapter = typesClient.fetchData(source, segments, types);
                        if (results.get(source) == null) {
                            Map<List<String>, DASTYPES> response = new HashMap<List<String>, DASTYPES>();
                            response.put(segments, adapter);
                            results.put(source, response);
                        } else {
                            results.get(source).put(segments, adapter);
                        }
                        logger.debug("Response from " + source + " has been already retrieved");
                    } catch (DASClientException e) {
                        if (errors.get(source) == null) {
                            Map<List<String>, Exception> response = new HashMap<List<String>, Exception>();
                            response.put(segments, e);
                            errors.put(source, response);
                        } else {
                            errors.get(source).put(segments, e);
                        }
                        logger.error("Fetching data from source " + source + " raised an exception: " + e.getMessage());
                    }
                }
            });
        }
        while (!queue.isEmpty() ) {
            logger.info("Waiting for " + queue.size() + " jobs");
        }
    }

    /**
     * Fetches data from multiple sources for a list of segments; segments can include ranges.
     * The invoking method is responsible for processing the results.
     * @param sources List of sources (url)
     * @param results Map of (source name, DASTYPES responses) one for each source successfully queried
     * @param errors  Map of (source name, exceptions) one for each source raising an exception
     */
    public void fetchData(List<String> sources, final Map<String, DASTYPES> results, final Map<String, Exception> errors) {
        for (final String source: sources) {
            this.runTask(new Runnable() {
                @Override
                public void run() {
                    try {
                        TypesClient typesClient = new TypesClient();
                        DASTYPES adapter = typesClient.fetchData(source);
                        results.put(source, adapter);
                        logger.debug("Response from " + source + " has been already retrieved");
                    } catch (DASClientException e) {
                        errors.put(source, e);
                        logger.error("Fetching data from source " + source + " raised an exception: " + e.getMessage());
                    }
                }
            });
        }
        while (!queue.isEmpty() ) {
            logger.info("Waiting for " + queue.size() + " jobs");
        }
    }
}
